//
//  DYRoleDataManager.swift
//  Chatup
//
//  Created by op on 2023/10/17.
//

import Foundation
import WCDBSwift
import KakaJSON

class RoleDataManager{
    static let share = RoleDataManager()
    
    
    func getRoles() -> [RoleModel] {
        let models = ChatUpWCDBWrapLoader.share.qureyFromDbArray(ttype: RoleModel.self,orderBy: [RoleModel.Properties.categoryId.asOrder(by: .ascending)])
        return models ?? []
    }
    
    func getRoles(category: String) -> [RoleModel] {
        if category == "Popular"{
            let models = ChatUpWCDBWrapLoader.share.qureyFromDbArray(ttype: RoleModel.self,where: RoleModel.Properties.id.is(14)||RoleModel.Properties.id.is(3)||RoleModel.Properties.id.is(5)||RoleModel.Properties.id.is(1)||RoleModel.Properties.id.is(11)||RoleModel.Properties.id.is(15)||RoleModel.Properties.id.is(18)||RoleModel.Properties.id.is(22))
            return models ?? []
        }
        
        
        let models = ChatUpWCDBWrapLoader.share.qureyFromDbArray(ttype: RoleModel.self,where: RoleModel.Properties.category.is(category),orderBy: [RoleModel.Properties.categoryId.asOrder(by: .ascending) , RoleModel.Properties.id.asOrder(by: .ascending)])
        return models ?? []
    }

    func getRole(roleId:Int) -> RoleModel? {
        return ChatUpWCDBWrapLoader.share.qureyFromDb(ttype: RoleModel.self,where: RoleModel.Properties.serverId.is(roleId))
    }
    
    func getRole(roleType:Int) -> RoleModel?{
        let model = ChatUpWCDBWrapLoader.share.qureyFromDb(ttype: RoleModel.self,where: RoleModel.Properties.roleType.is(roleType))
        return model
    }
    
    func saveRole(role:RoleModel) {
        ChatUpWCDBWrapLoader.share.insertToDb(object: role)
    }
    func saveRoles(roles:[RoleModel]) -> Bool{
        return ChatUpWCDBWrapLoader.share.insertToDbArray(objects:roles)
    }
    
    func updateRole(role:RoleModel){
        ChatUpWCDBWrapLoader.share.updateToDb(object: role, on: [RoleModel.Properties.imageIndex],where: RoleModel.Properties.id.is(role.id))
    }
}

